package top.minuy.util.helper;

import top.minuy.algorithm.sort.bubble.BubbleSort;
import top.minuy.algorithm.sort.heap.HeapSort;
import top.minuy.algorithm.sort.insertion.InsertionSort;
import top.minuy.algorithm.sort.merge.MergeBuSort;
import top.minuy.algorithm.sort.merge.MergeSort;
import top.minuy.algorithm.sort.quick.QuickSort;
import top.minuy.algorithm.sort.select.SelectSort;
import top.minuy.algorithm.sort.shell.ShellSort;
import top.minuy.util.constant.SortType;
/**
 * @author Minuy
 * @time 22:51
 * @date 2021/8/12
 */
public class SortingHelper {

    private SortingHelper(){}

    public static <E extends Comparable<E>> boolean isSorted(E[] arr) {
        for (int i = 1; i < arr.length; i++) {
            if(arr[i-1].compareTo(arr[i]) > 0){
                return false;
            }
        }
        return true;
    }

    public static <E extends Comparable<E>> void SortTest(SortType type, E[] arr){

        long startTime = System.nanoTime();
        if (type == SortType.SelectSort) {
            SelectSort.sort(arr);
        }else if(type == SortType.SelectSort2){
            SelectSort.sort2(arr);
        }else if(type == SortType.InsertionSort){
            InsertionSort.sort(arr);
        }else if(type == SortType.InsertionSort2){
            InsertionSort.sort2(arr);
        }else if(type == SortType.MergeSort){
            MergeSort.sort(arr);
        }else if(type == SortType.MergeBuSort){
            MergeBuSort.sort(arr);
        }else if(type == SortType.QuickSort_v1){
            QuickSort.sort(arr,1);
        }else if(type == SortType.QuickSort_v2){
            QuickSort.sort(arr,2);
        }else if(type == SortType.QuickSort_2w){
            QuickSort.sort(arr,3);
        }else if(type == SortType.QuickSort_3w){
            QuickSort.sort(arr,4);
        }else if(type == SortType.HeapSort){
            HeapSort.sort(arr);
        }else if(type == SortType.HeapSort2){
            HeapSort.sort2(arr);
        }else if(type == SortType.BubbleSort){
            BubbleSort.sort(arr);
        }else if(type == SortType.BubbleSort1){
            BubbleSort.sort1(arr);
        }else if(type == SortType.BubbleSort2){
            BubbleSort.sort2(arr);
        }else if(type == SortType.ShellSort){
            ShellSort.sort(arr);
        }else if(type == SortType.ShellSort2){
            ShellSort.sort2(arr);
        }else if(type == SortType.ShellSort3){
            ShellSort.sort3(arr);
        }


        long endTime = System.nanoTime();

        boolean isTrue = SortingHelper.isSorted(arr);

        double time = (endTime - startTime) / 1000000000.0;
        System.out.println(type.toString()+" : n = " + arr.length + ", check = "+ isTrue +" : " + time + " s");
    }

}
